// var baseUrl = "https://wx.xiechengyun.cn";
var baseUrl = "http://localhost";
var appid = "wx7351741807a08e59";
//app.js
App({
  url: {
    getOpenid: baseUrl + "/wx/user/" + appid + "/login",
    updateUserInfo: baseUrl + "/wx/user/" + appid + "/info",
    getUserMobile: baseUrl + "/wx/user/" + appid + "/phone",
    auth: baseUrl + "/wx/user/" + appid + "/auth",
    getListData: baseUrl + "/wx/dispatch/" + appid + "/listData",
    getAllListData: baseUrl + "/wx/dispatch/" + appid + "/allListData",
    doOpration: baseUrl + "/wx/dispatch/" + appid + "/doOperation",
    getCount: baseUrl + "/wx/dispatch/" + appid + "/getCount",
    getNewCount: baseUrl + "/wx/dispatch/" + appid + "/getNewCount",
    getDetail: baseUrl + "/wx/dispatch/" + appid + "/getDetail",
    getTodaySignInData: baseUrl + "/wx/signIn/" + appid + "/getTodaySignInData",
    doSignIn: baseUrl + "/wx/signIn/" + appid + "/doSignIn"
  },
  onLaunch: function() {
    wx.getSystemInfo({
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;
        let custom = wx.getMenuButtonBoundingClientRect();
        this.globalData.Custom = custom;
        this.globalData.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
        this.globalData.screenHeight = e.screenHeight / (e.screenWidth / 750);
      }
    })
    //检测版本
    if (this.checkVersion()) {
      //登录
      this.getOpenid();
    }
  },
  //登录方法,去后台获取openid和sessionid,缓存到全局变量中
  getOpenid() {
    wx.showLoading({
      title: '请稍后...',
      mask: true
    });
    var that = this;
    // 登录
    wx.login({
      success: res => {
        wx.request({
          url: this.url.getOpenid,
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: {
            code: res.code
          },
          success: function(res) {
            const data = res.data;
            if (data.result == "true") {
              if (data.message == 'isLogon') {
                that.globalData.openid = data.data.openid;
                that.globalData.sessionId = data.data.sessionId;
                that.globalData.loginState = true;
                that.globalData.userInfo = data.data;
                wx.hideLoading();
              } else {
                that.globalData.openid = data.data.openid;
                that.globalData.sessionId = data.data.sessionId;
                that.globalData.loginState = false;
                wx.hideLoading();
              }
            } else {
              wx.hideLoading();
              that.globalData.loginState = false;
              wx.showModal({
                content: '登录失败,请将微信彻底关闭后再次打开尝试,如继续失败,请直接联系您的服务商.',
                showCancel: false,
                confirmText: "重试",
                success: function(res) {
                  that.getOpenid();
                }
              });
            }
          },
          fail: res => {
            wx.hideLoading();
            wx.showModal({
              content: '登录失败,请检查网络,或将微信彻底关闭后再次打开尝试,如继续失败,请直接联系您的服务商.',
              showCancel: false,
              confirmText: "重试",
              success: function(res) {
                that.getOpenid();
              }
            });
          }
        });
      }
    });
  },
  globalData: {
    userInfo: null,
    loginState: false,
    openid: null,
    sessionId: null,
    screenHeight: null,
    ColorList: [{
        title: '嫣红',
        name: 'red',
        color: '#e54d42'
      },
      {
        title: '桔橙',
        name: 'orange',
        color: '#f37b1d'
      },
      {
        title: '明黄',
        name: 'yellow',
        color: '#fbbd08'
      },
      {
        title: '橄榄',
        name: 'olive',
        color: '#8dc63f'
      },
      {
        title: '森绿',
        name: 'green',
        color: '#39b54a'
      },
      {
        title: '天青',
        name: 'cyan',
        color: '#1cbbb4'
      },
      {
        title: '海蓝',
        name: 'blue',
        color: '#0081ff'
      },
      {
        title: '姹紫',
        name: 'purple',
        color: '#6739b6'
      },
      {
        title: '木槿',
        name: 'mauve',
        color: '#9c26b0'
      },
      {
        title: '桃粉',
        name: 'pink',
        color: '#e03997'
      },
      {
        title: '棕褐',
        name: 'brown',
        color: '#a5673f'
      },
      {
        title: '玄灰',
        name: 'grey',
        color: '#8799a3'
      },
      {
        title: '草灰',
        name: 'gray',
        color: '#aaaaaa'
      },
      {
        title: '墨黑',
        name: 'black',
        color: '#333333'
      },
      {
        title: '雅白',
        name: 'white',
        color: '#ffffff'
      },
    ]
  },
  checkVersion() {
    var that = this;
    //版本判断逻辑
    //用户小程序版本
    var userVer = wx.getSystemInfoSync().SDKVersion;
    //console.log(userVer);
    //目标小程序版本
    var destVer = "1.9.89";
    if (this.compareVersion(userVer, destVer) < 1) {
      // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
      wx.showModal({
        title: '提示',
        showCancel: false,
        confirmText: "知道了",
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。',
        success() {
          that.checkVersion();
        }
      });
      return false;
    }
    return true;
  },
  //版本比对
  compareVersion(v1, v2) {
    v1 = v1.split('.');
    v2 = v2.split('.');
    var len = Math.max(v1.length, v2.length);
    while (v1.length < len) {
      v1.push('0');
    }
    while (v2.length < len) {
      v2.push('0');
    }
    for (var i = 0; i < len; i++) {
      var num1 = parseInt(v1[i])
      var num2 = parseInt(v2[i])
      if (num1 > num2) {
        return 1;
      } else if (num1 < num2) {
        return -1;
      }
    }
    return 0;
  }
})